import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from '@/store/user'

// 路由配置
const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login.vue'),
        meta: {
            title: '登录',
            requiresAuth: false
        }
    },
    {
        path: '/',
        name: 'Layout',
        component: () => import('@/layout/index.vue'),
        redirect: '/dashboard',
        meta: {
            requiresAuth: true
        },
        children: [
            {
                path: '/dashboard',
                name: 'Dashboard',
                component: () => import('@/views/Dashboard.vue'),
                meta: {
                    title: '仪表盘',
                    requiresAuth: true,
                    icon: 'DataBoard'
                }
            },
            // 用户管理
            {
                path: '/users',
                name: 'UserManagement',
                component: () => import('@/views/user/UserList.vue'),
                meta: {
                    title: '用户管理',
                    requiresAuth: true,
                    icon: 'User'
                }
            },
            // 商品分类管理
            {
                path: '/categories',
                name: 'CategoryManagement',
                component: () => import('@/views/category/CategoryList.vue'),
                meta: {
                    title: '分类管理',
                    requiresAuth: true,
                    icon: 'Menu'
                }
            },
            // 商品管理
            {
                path: '/products',
                name: 'ProductManagement',
                component: () => import('@/views/product/ProductList.vue'),
                meta: {
                    title: '商品管理',
                    requiresAuth: true,
                    icon: 'Goods'
                }
            },
            {
                path: '/products/create',
                name: 'ProductCreate',
                component: () => import('@/views/product/ProductForm.vue'),
                meta: {
                    title: '添加商品',
                    requiresAuth: true,
                    hidden: true
                }
            },
            {
                path: '/products/edit/:id',
                name: 'ProductEdit',
                component: () => import('@/views/product/ProductForm.vue'),
                meta: {
                    title: '编辑商品',
                    requiresAuth: true,
                    hidden: true
                }
            },
            // 订单管理
            {
                path: '/orders',
                name: 'OrderManagement',
                component: () => import('@/views/order/OrderList.vue'),
                meta: {
                    title: '订单管理',
                    requiresAuth: true,
                    icon: 'Document'
                }
            },
            {
                path: '/orders/:id',
                name: 'OrderDetail',
                component: () => import('@/views/order/OrderDetail.vue'),
                meta: {
                    title: '订单详情',
                    requiresAuth: true,
                    hidden: true
                }
            },
            // 评论管理
            {
                path: '/comments',
                name: 'CommentManagement',
                component: () => import('@/views/comment/CommentList.vue'),
                meta: {
                    title: '评论管理',
                    requiresAuth: true,
                    icon: 'ChatDotRound'
                }
            },
            // 个人信息
            {
                path: '/profile',
                name: 'Profile',
                component: () => import('@/views/profile/ProfilePage.vue'),
                meta: {
                    title: '个人信息',
                    requiresAuth: true,
                    icon: 'User',
                    hidden: true // 不在侧边栏显示，通过头部用户菜单访问
                }
            }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        redirect: '/dashboard'
    }
]

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    const token = userStore.token

    // 设置页面标题
    if (to.meta.title) {
        document.title = `${to.meta.title} - Easy Mart 管理系统`
    }

    // 如果访问登录页面且已登录，重定向到首页
    if (to.path === '/login' && token) {
        next('/dashboard')
        return
    }

    // 如果需要认证且未登录，重定向到登录页
    if (to.meta.requiresAuth && !token) {
        next('/login')
        return
    }

    next()
})

export default router
